/**
 * create by sanbz
 */

package com.icangya.admin;

import com.alibaba.fastjson.JSONObject;
import com.icangya.DAO.IGainRecordDAO;
import com.icangya.Utils.JSONUtils;
import com.icangya.domain.GainRecord;
import com.icangya.service.IGainRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.Timestamp;
import java.util.List;


@CrossOrigin
@RestController
@RequestMapping(value = "/gain")
public class GainController {

    @Autowired
    private IGainRecordService iGainRecordService;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public JSONObject getAllGainRecord(@RequestParam int page){
        if(page<=0){
            return JSONUtils.getRoot(-1,"页码需大于0","");
        }
        List<GainRecord> resultSet = iGainRecordService.getAllGain(page);
        if(resultSet.size()==0){
            return JSONUtils.getRoot(-2,"还未添加记录","");
        }
        JSONObject json=new JSONObject();
        json.put("code",0);
        json.put("msg","success");
        json.put("data",resultSet.get(0));
        json.put("total",resultSet.get(1));
        return json;
    }

//    //startpri
//    @Autowired
//    /*
//    @RequestMapping(value = "/test",method = RequestMethod.GET)
//    public JSONObject getByTime(@RequestParam long startTime,@RequestParam long endTime){
//        Timestamp sT=new Timestamp(startTime);
//        Timestamp eT=new Timestamp(endTime);
//        return JSONUtils.getRoot(0,"success",iGainRecordDAO.getGainsByTime(sT,eT));
//    }*/
//
//    @RequestMapping(value = "/save",method = RequestMethod.GET)
//    public JSONObject saveNow(){
//
//        return JSONUtils.getRoot(0,"success",iGainRecordService.saveOne());
//    }
//    //endpri

    @RequestMapping(value = "/time",method = RequestMethod.GET)
    public JSONObject getGainsRecordBytime(@RequestParam long startTime,@RequestParam long endTime){
        //请求
        //http://192.168.0.105:8080/gain/time?startTime=1506272400000&endTime=1506272400000

//        try{
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//
//            Date start = sdf.parse(startTime);
//            Date end = sdf.parse(endTime);
//        }catch (ParseException e){
//            e.printStackTrace();
//        }
        /**
         * 时间戳需满足13位
         */

        if(startTime==0||endTime==0){
            return JSONUtils.getRoot(-1,"未指定时间","");
        }

        if((new Long(startTime)).toString().length()!=13||(new Long(endTime)).toString().length()!=13){
            return JSONUtils.getRoot(-2,"时间格式错误，请使用毫秒级时间戳",startTime+" "+endTime);
        }

        if(endTime < startTime){
            return JSONUtils.getRoot(-3,"开始时间与结束时间调转",startTime+" "+endTime);
        }
        if(System.currentTimeMillis()<startTime || System.currentTimeMillis()<endTime){
            return JSONUtils.getRoot(-4,"未来时间",startTime+" "+endTime);
        }

        Timestamp sT=new Timestamp(startTime);
        Timestamp eT=new Timestamp(endTime);



        GainRecord result = iGainRecordService.getGainByTimeSlot(sT,eT);
        Timestamp currentTt=new Timestamp(System.currentTimeMillis());
        result.setCreateTime(currentTt);
        if(result==null){
            return JSONUtils.getRoot(-1,"还未添加记录","");
        }

        return JSONUtils.getRoot(0,"success",result);
    }

}
